package com.Controller;

import com.Service.LoginService;
import com.entity.Login;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private LoginService loginService;

    @RequestMapping(value = "/insert",method = RequestMethod.POST)
    public Object insert(@RequestBody Login login){
        int record = loginService.insert(login);
        if(record ==0){
            return "faild";
        }
        return "success";
    }

    /**
     * 查询所有用户信息
     * @param login
     * @return
     */
    @RequestMapping(value = "/query",method = RequestMethod.GET)
    public Object query(Login login){
        List<Login> loginList = loginService.query(login);
        return loginList;
    }

    /**
     * 检验登录信息，是否成功
     * @param login
     * @param session
     * @return
     */
    @RequestMapping(value = "/check",method = RequestMethod.GET)
    public Object check(Login login,HttpSession session){
        System.out.println(login);
        List<Login> result = loginService.check(login);
        if(result.isEmpty()){
            return 0;
        }
        session.setAttribute("name",login.getName());
        return 1;
    }
}
